<?php
App::uses('AppController', 'Controller');
/**
 * Plantillas Controller
 *
 * @property Plantilla $Plantilla
 */
class PlantillasController extends AppController {
	public $uses = array( 'Plantilla', 'Criterio');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Plantilla->recursive = 0;
		$this->set('plantillas', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Plantilla->exists($id)) {
			throw new NotFoundException(__('Plantilla inválida'));
		}
		$options = array('conditions' => array('Plantilla.' . $this->Plantilla->primaryKey => $id));
		$this->set('plantilla', $this->Plantilla->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Plantilla->create();
			if ($this->Plantilla->save($this->request->data)) {
				$this->Session->setFlash(__('La plantilla ha sido guardada'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar la plantilla. Por favor intente denuevo.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Plantilla->exists($id)) {
			throw new NotFoundException(__('Plantilla inválida'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Plantilla->save($this->request->data)) {
				$this->Session->setFlash(__('La plantilla ha sido guardada'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar la plantilla. Por favor intente denuevo.'));
			}
		} else {
			$options = array('conditions' => array('Plantilla.' . $this->Plantilla->primaryKey => $id));
			$this->request->data = $this->Plantilla->find('first', $options);
		}
	}
	public function add_criterio($id) {
        $this->request->data['Criterio']['plantilla_id'] = $id;
		if ($this->request->is('post')) {
			$this->Criterio->create();
			if ($this->Criterio->save($this->request->data)) {
				$this->Session->setFlash(__('El criterio ha sido guardado'));
				$this->redirect('/plantillas/view/'.$this->data['Criterio']['plantilla_id']);
			} else {
				$this->Session->setFlash(__('No se ha podido guardar el criterio. Por favor intente denuevo.'));
			}
		}

		$plantillas = $this->Criterio->Plantilla->find('list');
		$this->set(compact('plantillas'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Plantilla->id = $id;
		if (!$this->Plantilla->exists()) {
			throw new NotFoundException(__('Plantilla inválida'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Plantilla->delete()) {
			$this->Session->setFlash(__('Plantilla eliminada'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Plantilla no fue eliminada'));
		$this->redirect(array('action' => 'index'));
	}
}
